<template>
	<view class="container" :style="appThemeStyle">
		<!-- 标题 -->
		<view class="page-title">请选择要登陆的账号</view>
		<!-- 表单组件 -->
		<view class="form-wrapper">
		              
				<view v-for="(item, index) in userInfo" :key="index" @click="otherLogin(item.user_id)" @longpress="onLongPress(item.id)" class="user_view">
					<view>账号：{{ nameFilter(item.user.mobile,item.user.nick_name) }}</view>
					<view v-if="item.user_id == currentId" class="red-dot" style="margin-left: auto;"></view>
					<view v-if="item.default" style="margin-left: auto; color: darkgray;">默认</view>
				</view>		
					
		</view>
		<view class="page-title">关联依据：<br>使用同一微信完成过交易支付或从此页面进行注册。</view>
		<!-- 操作按钮 -->
		<view class="footer">
			<view class="btn-wrapper">
				<view class="btn-item btn-item-wechat" @click="add()">注册关联账号</view>
			</view>
		</view>
	</view>
</template>

<script>
	import store from '@/store'
	import * as UserApi from '@/api/user'
    import { isEmpty } from '../../../utils/util'

	export default {
		data() {
			return {
				userInfo: null,
				currentId: null,
				defaultUserId: 0
			}
		},

		/**
		 * 生命周期函数--监听页面加载
		 */
		async onLoad() {
			// 获取当前用户信息
			await this.getUserInfo()
		},

		methods: {
			nameFilter (value, value2,) {
			      if (value) {
			        return value
			      } else {
					return value2
				  }
			    },
			// 获取当前用户信息
			getUserInfo() {
				const app = this
				UserApi.otherUsers()
					.then(result => {
						const users = result.data.userOauth.users
						app.userInfo = users
						app.currentId = result.data.userOauth.current_id
						if (!isEmpty(users)) {
						    users.forEach(item => {
							    if (item.default == 1) {
									app.defaultUserId = item.id
									console.log('defaultUserId', app.defaultUserId)
									return true
								}
						    })
						}
					})
			},
            // 切换登陆
			otherLogin (userId) {
				UserApi.otherLogin({
					userId: userId
				})
				.then(setTimeout(() => {this.$navTo('pages/user/index')}, 500))
			},
			// 注册账号
			add () {
				this.$navTo('pages/user/personal/add')
			},
			// 长按事件
			onLongPress(id) {
			      console.log('长按事件触发');
			      // 在这里执行长按后的操作
				  const app = this
				  uni.showModal({
				  	content: '将此设为默认账号吗?',
				  	success(res) {
				  		if (res.confirm) {
				  			UserApi.setDefaultUser({
								id: id,
								defaultId: app.defaultUserId
							})
				  			.then(result => app.getUserInfo())
				  		}
				  	}
				  })
			}
			
		}
	}
</script>

<style>
	page {
		background: #f7f8fa;
	}
</style>
<style lang="scss" scoped>
	.user_view {
		display: flex;
		width: 100%;
		height: 60px;
		padding-top: 20px;
		border-bottom: 1px solid #f3f3f3;
	}
	.container {}

	.page-title {
		width: 94%;
		margin: 0 auto;
		padding-top: 40rpx;
		font-size: 28rpx;
		color: rgba(69, 90, 100, 0.6);
	}

	.form-wrapper {
		margin: 20rpx auto 20rpx auto;
		padding: 0 40rpx;
		width: 94%;
		box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
		border-radius: 16rpx;
		background: #fff;	
	}
	.red-dot {
	  margin-left: 20px;
	  width: 15px; /* 圆点的宽度 */
	  height: 15px; /* 圆点的高度，可以和宽度保持一致 */
	  background-color: #2eac0b; /* 红色的十六进制代码 */
	  border-radius: 50%; /* 圆形的关键点是将border-radius设置成50% */
	}

	/* 底部操作栏 */

	.footer {
		margin-top: 80rpx;

		.btn-wrapper {
			height: 100%;
			// display: flex;
			// align-items: center;
			padding: 0 20rpx;
		}

		.btn-item {
			flex: 1;
			font-size: 28rpx;
			height: 86rpx;
			color: #fff;
			border-radius: 50rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.btn-item-wechat {
			background: #0ba90b;
			margin-bottom: 26rpx;
		}
		
		.btn-item-user {
			background: #d3d3d3;
			margin-bottom: 26rpx;
		}

		.btn-item-main {
			background: linear-gradient(to right, $main-bg, $main-bg2);
			color: $main-text;
		}
		

	}
</style>